package com.sky.aspect;

import com.sky.annotation.ActionLog;
import com.sky.context.BaseContext;

import com.sky.entity.AdminActionLog;
import com.sky.service.AdminLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
@Aspect
@Component
public class ActionLogAspect {

    @Autowired
    private AdminLogService logService;

    private ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Before("@annotation(com.sky.annotation.ActionLog)")
    public void before(JoinPoint joinPoint) {
        startTime.set(System.currentTimeMillis());
    }

    @AfterReturning(pointcut = "@annotation(com.sky.annotation.ActionLog)", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        saveLog(joinPoint, true, null);
    }

    @AfterThrowing(pointcut = "@annotation(com.sky.annotation.ActionLog)", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Throwable ex) {
        saveLog(joinPoint, false, ex.getMessage());
    }

    private void saveLog(JoinPoint joinPoint, boolean success, String errorMsg) {
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();

            ActionLog actionLog = method.getAnnotation(ActionLog.class);
            String action = actionLog.value();
            String module = actionLog.module();

            // 获取请求信息
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();

            // 获取当前用户
           Integer uId=BaseContext.getCurrentId();

            // 构建日志对象
            AdminActionLog log = new AdminActionLog();
            log.setUId(uId);
            log.setAction(action);
            log.setModule(module);
            log.setMethod(request.getMethod() + " " + request.getRequestURI());
            log.setParams(joinPoint.getArgs()); // 存储原始参数
            log.setIp(request.getRemoteAddr());
            log.setSuccess(success);
            log.setErrorMsg(errorMsg);
            log.setCreateTime(new Date());
            log.setExecutionTime(System.currentTimeMillis() - startTime.get());

            // 保存日志到 MongoDB
            logService.saveLogAsync(log);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            startTime.remove();
        }
    }
}